package com.example.constellation.Fragment;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Spinner;

import com.example.constellation.Activity.fg2pdActivity;
import com.example.constellation.R;
import com.example.constellation.bean.StarBean;
import com.example.constellation.utils.AssetsUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * A simple {@link Fragment} subclass.
 */
public class Fragment2 extends Fragment implements View.OnClickListener {
    private ImageView fg2man,fg2woman;
    private Spinner fg2spman,fg2spwoman;
    private Button fg2but1,fg2but2;
    private List<StarBean.StarinfoBean> infolist;
    private List<String> namelist;   //存放星座名称
    private Map<String, Bitmap> contentlogomap;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

      View view=inflater.inflate(R.layout.fragment_2, container, false);
      initview(view);
      //获取mainactivity传来的数据
        Bundle bundle = getArguments();
        StarBean starBean= (StarBean) bundle.getSerializable("info");
        infolist = starBean.getStarinfo();
        namelist=new ArrayList<>();
        //获取适配器需要的数据源
        for (int i=0;i<infolist.size();i++){
            String name = infolist.get(i).getName();
            namelist.add(name);
        }
        //适配器
        ArrayAdapter<String> spiadapter = new ArrayAdapter<>(getContext(), R.layout.fg2spinner, R.id.spinnertv, namelist);
        fg2spman.setAdapter(spiadapter);
        fg2spwoman.setAdapter(spiadapter);
        //默认显示第一个图片
        String logoname = infolist.get(0).getLogoname();
        contentlogomap = AssetsUtils.getContentlogomap();
        Bitmap bitmap = contentlogomap.get(logoname);
        fg2man.setImageBitmap(bitmap);
        fg2woman.setImageBitmap(bitmap);
        return view;
    }

    private void initview(View view) {
        fg2but1=view.findViewById(R.id.fg2but1);
        fg2but2=view.findViewById(R.id.fg2but2);
        fg2man=view.findViewById(R.id.fg2man);
        fg2woman=view.findViewById(R.id.fg2woman);
        fg2spman=view.findViewById(R.id.fg2spman);
        fg2spwoman=view.findViewById(R.id.fg2spwoman);
        fg2but1.setOnClickListener(this);
        fg2but2.setOnClickListener(this);
        fg2spwoman.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String s = infolist.get(position).getLogoname();
                Bitmap bitmap = contentlogomap.get(s);
                fg2woman.setImageBitmap(bitmap);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        fg2spman.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String s = infolist.get(position).getLogoname();
                Bitmap bitmap = contentlogomap.get(s);
                fg2man.setImageBitmap(bitmap);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fg2but1:
                break;
            case R.id.fg2but2:
                //获取spinner选择的位置
                int manspin=fg2spman.getSelectedItemPosition();
                int womanspin=fg2spwoman.getSelectedItemPosition();
                //跳转传值
                Intent intent = new Intent(getContext(), fg2pdActivity.class);
                intent.putExtra("man_name",infolist.get(manspin).getName());
                intent.putExtra("man_logoname",infolist.get(manspin).getLogoname());
                intent.putExtra("woman_name",infolist.get(womanspin).getName());
                intent.putExtra("woman_logoname",infolist.get(womanspin).getLogoname());
                startActivity(intent);
                break;
        }
    }
}
